package com.x.erp.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.x.erp.model.LoanClue;
import com.x.framework.dao.BaseDao;
/**
 * 贷款线索dao
 * @author pansong
 *
 */
@Repository
public class LoanClueDao extends BaseDao {

	/**
	 * 贷款线索列表
	 * 
	 * @param loanClue
	 * @return
	 */
	private final static String selectLoanClueListSql = "SELECT T1.*,T2.MODEL_NAME "
			+ "FROM ERP_LOAN_CLUE T1 "
			+ "LEFT JOIN x_base.BASE_MODEL T2 ON T1.MODEL_ID=T2.MODEL_ID "
			+ "WHERE 1 = 1 ";

	public List<LoanClue> selectLoanClueList(LoanClue loanClue) throws Exception {
		StringBuilder sql = new StringBuilder(selectLoanClueListSql);
		List<Object> params = new ArrayList<>();
		selectSearch(sql, params, loanClue);
		sql.append("ORDER BY LOAN_CREATE_TIME DESC ");
		return this.queryForList(sql.toString(), LoanClue.class, params, loanClue);
	}
	/**
	 * 贷款线索总数
	 * 
	 * @param insuranceClue
	 * @return
	 */
	private final static String selectLoanClueNumSql = "SELECT COUNT(T1.LOAN_CLUE_ID)  FROM ERP_LOAN_CLUE T1 WHERE 1 = 1 ";

	public int selectLoanClueNum(LoanClue loanClue) {
		StringBuilder sql = new StringBuilder(selectLoanClueNumSql);
		List<Object> params = new ArrayList<>();
		selectSearch(sql, params, loanClue);
		return this.queryForInt(sql.toString(), params);
	}

	/**
	 * 拼接查询条件
	 */
	private void selectSearch(StringBuilder sql, List<Object> params, LoanClue loanClue) {
		if (this.isNotNull(loanClue.getLoanClueId())) {
			sql.append("AND T1.LOAN_CLUE_ID = ? ");
			params.add(loanClue.getLoanClueId());
		}
		if (this.isNotNull(loanClue.getLoanType())) {
			sql.append("AND T1.LOAN_TYPE = ? ");
			params.add(loanClue.getLoanType());
		}
		if (this.isNotNull(loanClue.getModelId())) {
			sql.append("AND T1.MODEL_ID = ? ");
			params.add(loanClue.getModelId());
		}
		if (this.isNotNull(loanClue.getLoanPeriod())) {
			sql.append("AND T1.LOAN_PERIOD = ? ");
			params.add(loanClue.getLoanPeriod());
		}
		if (this.isNotNull(loanClue.getLoanTime())) {
			sql.append("AND T1.LOAN_TIME = ? ");
			params.add(loanClue.getLoanTime());
		}
	}
}
